#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "string_atoi.h"

int string_atoi(char* a) {
	int sign = 1;
	int value = 0;
	int length = strlen(a);
	int index = 0;

	if (a[index] == '-') {
		sign = -1;
		index++;
	}	
	while (index < length) {
		assert((a[index] >= '0') && (a[index] <= '9'));
		value = value * 10 + (a[index] - '0');
		index++;
	} 
	return sign * value;
}

void test_string_atoi() {
	char a[] = "49278";
	printf("Original string: %s\n", a);
	int value = string_atoi(a);
	printf("Integer value = %d\n\n", value);
	
	char b[] = "-509823";
	printf("Original string: %s\n", b);
	value = string_atoi(b);
	printf("Integer value = %d\n\n", value);
}
